package com.ruoyi.md.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.md.domain.MdUnitMeasure;
import com.ruoyi.md.service.IMdUnitMeasureService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 单位Controller
 *
 * @author ruoyi
 * @date 2025-10-15
 */
@RestController
@RequestMapping("/md/measure")
public class MdUnitMeasureController extends BaseController
{
    @Autowired
    private IMdUnitMeasureService mdUnitMeasureService;

    /**
     * 查询单位列表
     */
    @PreAuthorize("@ss.hasPermi('md:measure:list')")
    @GetMapping("/list")
    public TableDataInfo list(MdUnitMeasure mdUnitMeasure)
    {
        startPage();
        List<MdUnitMeasure> list = mdUnitMeasureService.selectMdUnitMeasureList(mdUnitMeasure);
        return getDataTable(list);
    }

    /**
     * 导出单位列表
     */
    @PreAuthorize("@ss.hasPermi('md:measure:export')")
    @Log(title = "单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MdUnitMeasure mdUnitMeasure)
    {
        List<MdUnitMeasure> list = mdUnitMeasureService.selectMdUnitMeasureList(mdUnitMeasure);
        ExcelUtil<MdUnitMeasure> util = new ExcelUtil<MdUnitMeasure>(MdUnitMeasure.class);
        util.exportExcel(response, list, "单位数据");
    }

    /**
     * 获取单位详细信息
     */
    @PreAuthorize("@ss.hasPermi('md:measure:query')")
    @GetMapping(value = "/{measureId}")
    public AjaxResult getInfo(@PathVariable("measureId") Long measureId)
    {
        return success(mdUnitMeasureService.selectMdUnitMeasureByMeasureId(measureId));
    }

    /**
     * 新增单位
     */
    @PreAuthorize("@ss.hasPermi('md:measure:add')")
    @Log(title = "单位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MdUnitMeasure mdUnitMeasure)
    {
        String primaryFlag = mdUnitMeasure.getPrimaryFlag();
        String enableFlag = mdUnitMeasure.getEnableFlag();

        if (!"Y".equals(primaryFlag) && !"N".equals(primaryFlag)) {
            return AjaxResult.error("主单位状态只能是'是'或'否'");
        }
        if (!"Y".equals(enableFlag) && !"N".equals(enableFlag)) {
            return AjaxResult.error("启用状态只能是'启用'或'禁用'");
        }
        return mdUnitMeasureService.insertMdUnitMeasure(mdUnitMeasure) > 0
                ? AjaxResult.success()
                : AjaxResult.error();
    }

    /**
     * 修改单位
     */
    @PreAuthorize("@ss.hasPermi('md:measure:edit')")
    @Log(title = "单位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MdUnitMeasure mdUnitMeasure)
    {
        String primaryFlag = mdUnitMeasure.getPrimaryFlag();
        String enableFlag = mdUnitMeasure.getEnableFlag();
        if (!"Y".equals(primaryFlag) && !"N".equals(primaryFlag)) {
            return AjaxResult.error("主单位状态只能是'Y'或'N'");
        }
        if (!"Y".equals(enableFlag) && !"N".equals(enableFlag)) {
            return AjaxResult.error("启用状态只能是'Y'或'N'");
        }
        return mdUnitMeasureService.updateMdUnitMeasure(mdUnitMeasure) > 0
                ? AjaxResult.success()
                : AjaxResult.error();

    }

    /**
     * 删除单位
     */
    @PreAuthorize("@ss.hasPermi('md:measure:remove')")
    @Log(title = "单位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{measureIds}")
    public AjaxResult remove(@PathVariable Long[] measureIds)
    {
        return toAjax(mdUnitMeasureService.deleteMdUnitMeasureByMeasureIds(measureIds));
    }
    /**
     * 查询主单位
     */
    @PreAuthorize("@ss.hasPermi('md:measure:query')")
    @GetMapping("/primary")
    public AjaxResult getPrimaryMeasures() {
        return success(mdUnitMeasureService.selectPrimaryMeasures());
    }
}
